$(function() {
//	CodeApp.init();
	
	
});
var CodeApp = function(){
	return{
		//初始化js
		init:function(){
		CodeApp.bindEvent();
		// 手机号码验证  
		jQuery.validator.addMethod("isMobile", function(value, element) {  
		    var length = value.length;  
		    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;  
		    return this.optional(element) || (length == 11 && mobile.test(value));  
		}, "请正确填写您的手机号码");  
		},
		subFun:function(){
			mui.util.Ajax.asyncPOST(
				$("#APP_ROOT_PATH").val() + "discount/bargain",
				{
					"discountid":$("#DISCOUNT_ID").val(),
					"phone":$("#phone").val(),
					"code":$("#code").val(),
				},
				function(data){
				if(data==true){
							mui.util.Ajax.asyncPOST(
							$("#APP_ROOT_PATH").val() + "discount/price",
							{
								"discountid":$("#DISCOUNT_ID").val(),
							},
							function(data){
								if(data.ret==null){
								if(data.price!=null){
								DiscountMemberDetailsApp.modal(data.price);
								}
								}
							},
							function(){},
							function(){},
							function(){}
							);
					}else{
						layer.open({
						    content: data.message,
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
					});
					}
				},
				function(){
					layer.open({
						    content: '保存出错！',
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
					});
				},
				function(){},
				function(){
//					setTimeout(function(){
//						layer.closeAll();
//					},1000);
					
				}
			)
			return false;
		},
		isMobileMethod:function(phone){  
		    var regPhone = /^(13[0-9]|15[012356789]|18[0123456789]|147|145|17[0-9])\d{8}$/;  
		    if(regPhone.test(phone)){  
		        return true;  
		    }else{  
		        return false;  
		    }     
		},  
		bindEvent:function(){
			
			
			//表单验证
			$("#myFrom").validate({
                rules: {
                	DISCOUNT_ID:{"required":true },
					phone:{"required":true ,isMobile:true},
					code:{"required":true },
                },
                messages: {
                	DISCOUNT_ID:{required:"活动id为空"},
                	phone:{required:"手机号不能空"},
                	code:{required:"验证码不能空"},
                },
				errorPlacement: function(error, element) {   
                    var placement = $(element.parent(".con").find(".error_place"));
                    error.appendTo(placement.html(''));
                },  
				submitHandler:function(form){
					CodeApp.subFun();
					return false;
                }
            });
			//取消
			$("#cancel_btn").bind("click",function(){
				layer.closeAll();
			})
			//保存
			$("#save_btn").bind("click",function(){
				$("#myFrom").submit();
			})
			//发送短信
			$("#send_btn").bind("click",function(){
				if(!CodeApp.isMobileMethod($("#phone").val())){
					layer.open({
						    content: '手机格式不对',
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
					});
					return false;
				}
				mui.util.Ajax.asyncGET(
						$("#APP_ROOT_PATH").val() + "discount/code", {
							"phone": $("#phone").val()
						},
						function(data) {
//							console.log(data);
							if(data == true) {
								layer.open({
									    content: '发送成功',
									    skin: 'msg',
									    time: 2 //2秒后自动关闭
								});
								CodeApp.timer(60,$("#send_btn"));
							}else{
								layer.open({
										    content: data.message,
										    skin: 'msg',
										    time: 2 //2秒后自动关闭
									});
							}
						},
						function() {

						},
						function() {
							
						},
						function() {
						}
					)
			})
		},
		
		/**
		 * 倒计时
		 */
		timer:function(intDiff,obj){
			var interValObj;
			$(obj).html('60 秒后可重复操作');
			interValObj = window.setInterval(function(){
				if(intDiff <=1){
					$(obj).attr("disabled",false); 
					$(obj).html('免费获取验证码');
					window.clearInterval(interValObj);
					return;
				}
				$(obj).html(intDiff + ' 秒后可重复操作');
				intDiff--;
			},1000);
		},
	}
}();